﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoVanHocTapData.Concretes;
using CoVanHocTapData.Entities;
using CoVanHocTapProject.Infrastructure;

namespace CoVanHocTapProject.Controllers
{
    public class TrangChu_AdminController : Controller
    {
        #region repos
        NguoiDungRepository nguoiDungRepos = new NguoiDungRepository();

        #endregion

        public ActionResult Index()
        {
            if (Publics.adminDangNhap == null)
                return RedirectToAction("DangNhapAdmin", "TrangChu_Admin");
            
           return View();
        }

        public ActionResult ThoatAdmin()
        {
            Publics.adminDangNhap = null;
            return RedirectToAction("Index", "TrangChu_Admin");
        }

        public ActionResult ThongTinCaNhan()
        {

            return View();
        }

        public ActionResult DangNhapAdmin()
        {
            return View();
        }
        [HttpPost]
        public ActionResult DangNhapAdmin(FormCollection form)
        {
            var login = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("subDangNhap"));
            if (login != null)
            {
                string tenDN = form["txtTenDangNhap"];
                string matKhau = form["txtMatKhau"];
                if (nguoiDungRepos.KiemTraAdminDangNhap(tenDN, CoVanHocTapData.Infrastructure.EncryptPassword.encryptString(matKhau)))
                {
                    NguoiDung admin = nguoiDungRepos.LayAdminDangNhap(tenDN);
                    CoVanHocTapProject.Infrastructure.Publics.adminDangNhap = admin;

                    return RedirectToAction("Index", "TrangChu_Admin");
                }
                else
                {
                    ViewData["LoiDangNhap"] = "Sai tên đăng nhập hoặc mật khẩu";
                    return View(ViewData["LoiDangNhap"]);
                }
            }
            return View();
        }

        public ActionResult MenuAdmin()
        {
            return View();
        }
    }
}
